#include <docker.h>

int DockerContainer::nginxProxyServices(string name){
    VesselServices vs;
    vs.name=name;// 服务名称
    // 服务镜像
    const char* image=getenv("DOCKER_VESSEL_NGINX_PROXY_IMAGE");
    string simage;
    if(image==nullptr){
       simage="crpi-vb403qyxdijc0eq9.cn-guangzhou.personal.cr.aliyuncs.com/utlis/build:22_04_nginx_proxy_frpc";
    }else{
        simage=string(image);
    }
    cout<<"nginx 服务 镜像设置为: "<<simage<<endl;
    vs.image=simage;
    // ipv4
    const char* ipv4=getenv("DOCKER_VESSEL_NGINX_PROXY_IPV4");
    string sipv4;
    if(ipv4==nullptr){
        sipv4=this->getIp("246");
    }else{
        sipv4=this->getIp(string(ipv4));
    }
    cout<<"nginx 服务 ipv4 设置为: "<<sipv4<<endl;
    vs.ip=sipv4;

    // nginx http 端口
    const char* http_port=getenv("DOCKER_VESSEL_NGINX_PROXY_HTTP_PORT");
    if(http_port !=nullptr){
        vs.ports.push_back(string(http_port)+":80");
    }
    // nginx https 端口
    const char* https_port=getenv("DOCKER_VESSEL_NGINX_PROXY_HTTPS_PORT");
    if(https_port !=nullptr){
        vs.ports.push_back(string(https_port)+":443");
    }

    // dns 邮箱
    const char* dns_email=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_EMAIL");
    if(dns_email==nullptr){
         cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_EMAIL"<<endl;
         cerr<<"dns 邮箱未设置"<<endl;
         return -1;
    }
    cout<<"nginx 服务 dns email 设置为: "<<string(dns_email)<<endl;
    vs.envs.push_back("DNS_EMAIL="+string(dns_email));

    // dns token id
    const char* dns_token_id=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_TOKEN_ID");
    if(dns_token_id==nullptr){
        cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_TOKEN_ID"<<endl;
      cerr<<"dns token id 未设置"<<endl;
      return -1;
    }
    cout<<"nginx 服务 dns token id 设置为: "<<string(dns_token_id)<<endl;
    vs.envs.push_back("DNS_TOKEN_ID="+string(dns_token_id));

    // dns token
    const char* dns_token=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_TOKEN");
    if(dns_token==nullptr){
        cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_TOKEN"<<endl;
        cerr<<"dns token 未设置"<<endl;
        return -1;
    }
    cout<<"nginx 服务 dns token 设置为: "<<string(dns_token)<<endl;
    vs.envs.push_back("DNS_TOKEN="+string(dns_token));

    // DNS DOMAIN 域名
    const char* dns_domain=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_DOMAIN");
    if(dns_domain==nullptr){
       cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_DOMAIN"<<endl;
       cerr<<"dns domain 主域名未设置"<<endl;
       return -1;
    }
    cout<<"nginx 服务 dns domain 设置为: "<<string(dns_domain)<<endl;
    vs.envs.push_back("DNS_DOMAIN="+string(dns_domain));

    // DNS TYPE
    const char* dns_type=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_TYPE");
    if(dns_type==nullptr){
        cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_DNS_TYPE"<<endl;
        cerr<<"dns type 未设置"<<endl;
        return -1;
    }
   cout<<"nginx 服务 dns type 设置为: "<<string(dns_type)<<endl;
   vs.envs.push_back("DNS_TYPE="+string(dns_type));

    // PUBLIC IP
    const char* public_ip=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_PUBLIC_IP");
    if(public_ip==nullptr){
        cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_PUBLIC_IP"<<endl;
        cerr<<"public ip 未设置"<<endl;
        return -1;
    }
    cout<<"nginx 服务 public ip 设置为: "<<string(public_ip)<<endl;
    vs.envs.push_back("PUBLIC_IP="+string(public_ip));


    // FRP SERVER PORT
    const char* frp_server_port=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_FRP_SERVER_PORT");
    if(frp_server_port==nullptr){
        cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_FRP_SERVER_PORT"<<endl;
        cerr<<"frp server port 未设置"<<endl;
        return -1;
    }
    cout<<"nginx 服务 frp server port 设置为: "<<string(frp_server_port)<<endl;
    vs.envs.push_back("FRP_SERVER_PORT="+string(frp_server_port));

    // FRP SERVER METHOD
    const char* frp_server_method=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_FRP_SERVER_METHOD");
    if(frp_server_method==nullptr){
       cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_FRP_SERVER_METHOD"<<endl;
       cerr<<"frp server method 未设置"<<endl;
       return -1;
    }
    cout<<"nginx 服务 frp server method 设置为: "<<string(frp_server_method)<<endl;
    vs.envs.push_back("FRP_SERVER_METHOD="+string(frp_server_method));

    // FRP TOKEN
    const char* frp_token=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_FRP_TOKEN");
    if(frp_token==nullptr){
       cerr<<"设置变量 DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_FRP_TOKEN"<<endl;
       cerr<<"frp token 未设置"<<endl;
       return -1;
    }
    cout<<"nginx 服务 frp token 设置为: "<<string(frp_token)<<endl;
    vs.envs.push_back("FRP_TOKEN="+string(frp_token));

    // FRP LOCAL
    const char* frp_local_ip=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_FRP_LOCAL_IP");
    string sfrp_local_ip;
    if(frp_local_ip==nullptr){
        sfrp_local_ip=vs.ip;
    }else{
        sfrp_local_ip=string(frp_local_ip);
    }
    cout<<"nginx 服务 frp local ip 设置为: "<<sfrp_local_ip<<endl;
    vs.envs.push_back("FRP_LOCAL_IP="+sfrp_local_ip);

    // NGINX PROXY
    const char* nginx_proxy=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_NGINX_HTTPS_PROXY");
    if(nginx_proxy != nullptr){
    cout<<"nginx 服务 nginx proxy 设置为: "<<string(nginx_proxy)<<endl;
    vs.envs.push_back("NGINX_HTTPS_PROXY="+string(nginx_proxy));
    }else{
        try {
            string https_proxy;
            VesselServices nvs;
            // 检查键 "nginx_proxy" 是否存在
            char* key="nexus3";
            if (this->mVessel.count(key) == 1){
                 nvs = this->mVessel.at(key);
                 https_proxy="nexus3,http://"+nvs.ip+":8081";
            }
            key="alist";
            if (this->mVessel.count(key) == 1){
                 nvs = this->mVessel.at(key);
                 https_proxy+=";alist,http://"+nvs.ip+":5244";
            }
            key="aria2";
            if (this->mVessel.count(key) == 1){
                 nvs = this->mVessel.at(key);
                 https_proxy+=";aria2,http://"+nvs.ip+":80";

            }
            key="video";
            if (this->mVessel.count(key) == 1){
                 nvs = this->mVessel.at(key);
                 https_proxy+=";video,http://"+nvs.ip+":8080";
            }

            if (!https_proxy.empty()){
                cout<<"nginx 服务 nginx proxy 设置为: "<<https_proxy<<endl;
                vs.envs.push_back("NGINX_HTTPS_PROXY="+https_proxy);
            }

        } catch (const std::out_of_range& e) {
            std::cerr << "Key 查找失败: " << e.what() << std::endl;
        }
    }

    const char* local_resources=getenv("DOCKER_VESSEL_NGINX_PROXY_ENVIRONMENT_NGINX_LOCAL_RESOURCES");
    if(local_resources!=nullptr){
        cout<<"nginx 服务 nginx local resources 设置为: "<<string(local_resources)<<endl;
        vs.envs.push_back("NGINX_LOCAL_RESOURCES="+string(local_resources));
    }
    // VOLUMES NGINX
    const char* volumes_nginx=getenv("DOCKER_VESSEL_NGINX_PROXY_VOLUMES_NGINX");
    string svolumes_nginx;
    if(volumes_nginx != nullptr){
        svolumes_nginx=this->rootVolume+string(volumes_nginx)+":/etc/nginx";
        cout<<"nginx 服务 volumes nginx 设置为: "<<svolumes_nginx<<endl;
        vs.volumes.push_back(svolumes_nginx);
    }
    // VOLUMES CERTBOT
    const char* volumes_certbot=getenv("DOCKER_VESSEL_NGINX_PROXY_VOLUMES_CERTBOT");
    string svolumes_certbot;
    if(volumes_certbot !=nullptr){
        svolumes_certbot=this->rootVolume+string(volumes_certbot)+":/etc/certbot";
        cout<<"nginx 服务 volumes certbot 设置为: "<<svolumes_certbot<<endl;
        vs.volumes.push_back(svolumes_certbot);
    }
    // VOLUMES FRP
    const char* volumes_frp=getenv("DOCKER_VESSEL_NGINX_PROXY_VOLUMES_FRPC");
    string svolumes_frp;
    if(volumes_frp !=nullptr){
        svolumes_frp=this->rootVolume+string(volumes_frp)+":/etc/frpc";
        cout<<"nginx 服务 volumes frp 设置为: "<<svolumes_frp<<endl;
        vs.volumes.push_back(svolumes_frp);
    }
    this->mVessel.insert({name,vs});
    this->addServices(vs);

   return 0;
}


int DockerContainer::httpsServices(){
    // 检查键 "nginx_proxy" 是否存在
    const char* nginx_key="nginx_proxy";
    if (this->mVessel.count(nginx_key) == 0)return 0;
    try {
        VesselServices* nginx = &this->mVessel.at(nginx_key);
        std::cout << "name : " << nginx->name <<  std::endl;
        nginx->name="我被修改了";
        std::cout << "name : " << this->mVessel.at(nginx_key).name <<  std::endl;
    } catch (const std::out_of_range& e) {
        std::cerr << "Key not found: " << e.what() << std::endl;
    }

    return 0;
}







